package com.tanhua.server.controller;

import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @GetMapping
    public ResponseEntity findUserInfo(@RequestParam(required = false) Long userID,
                                       @RequestParam(required = false) Long huanxinID) {
        long id = UserHolder.getUser().getId();
        if (userID != null) {
            id = userID;
        } else if (huanxinID != null) {
            id = huanxinID;
        }
        return userInfoService.findUserInfo(id);
    }

    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo){
        return userInfoService.updateUserInfo(userInfo);
    }

    @PostMapping("/header")
    public ResponseEntity updateHeadPhoto(MultipartFile headPhoto) throws IOException {
        return userInfoService.updateHeadPhoto(headPhoto);
    }
    /**
     * 我的-通用设置-查询通用设置
     * GET /users/settings
     */
    @GetMapping("/settings")
    public ResponseEntity findSettings(){
        return userInfoService.findSettings();
    }

    @PostMapping("/questions")
    public ResponseEntity updateQuestion(@RequestBody Map<String,String>map) {
        String questionTxt = map.get("content");
        return userInfoService.updateQuestion(questionTxt);
    }
    /**
     * 我的-通用设置-通知设置-保存通知设置
     * POST/users/notifications/setting
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity saveSettings(@RequestBody Settings settings){
        return userInfoService.saveSettings(settings);
    }

    @GetMapping("/blacklist")
    public ResponseEntity findBlackList(@RequestParam(defaultValue = "1")int page,
                                        @RequestParam(defaultValue = "10")int pagesize) {
        return userInfoService.findBlackList(page, pagesize);
    }
    /**
     * 我的-通用设置-黑名单-移除黑名单
     * DELETE /users/blacklist/:uid
     */
    @DeleteMapping("/blacklist/{blackUserId}")
    public ResponseEntity removeBlackList(@PathVariable("blackUserId") Long blackUserId){
        return userInfoService.removeBlackList(blackUserId);
    }
    /**
     * 我的-通用设置-修改手机号-发送短信验证码
     * POST /users/phone/sendVerificationCode
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        return userInfoService.sendVerificationCode();
    }

    /**
     * 我的-通用设置-修改手机号-校验验证码
     * POST/users/phone/checkVerificationCode
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        String verificationCode = (String) map.get("verificationCode");
        return userInfoService.checkVerificationCode(verificationCode);
    }

    /**
     * 我的-通用设置-修改手机号-保存
     * POST/users/phone
     */
    @PostMapping("/phone")
    public ResponseEntity updateMobile(@RequestBody Map map){
        String newPhone = (String) map.get("phone");
        return userInfoService.updateMobile(newPhone);
    }

    @GetMapping("/counts")
    public ResponseEntity userLikeCounts() {
        return userInfoService.userLikeCounts();
    }

    @GetMapping("/friends/{type}")
    public ResponseEntity userLikeList(@PathVariable("type") Integer type,
                                       @RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "10") int pagesize){

        return userInfoService.userLikeList(type, page, pagesize);
    }
    @PostMapping("/fans/{fansId}")
    public ResponseEntity likeFans(@PathVariable("fansId") Long fansId){
        return userInfoService.likeFans(fansId);
    }

    @DeleteMapping("like/{uid}")
    public ResponseEntity delete(@PathVariable("uid") Long fansId) {
        return userInfoService.delete(fansId);
    }


}
